package com.heaven.luxuriant.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.heaven.luxuriant.R;
import com.heaven.luxuriant.activity.NewsDetailActivity;
import com.heaven.luxuriant.base.GlideApp;
import com.heaven.luxuriant.data.News;

import org.apache.commons.lang3.StringUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {

    private static final String TAG = "NewsAdapter";

    private Context context;

    private List<News> news;

    public NewsAdapter(Context context, List<News> news) {
        this.context = context;
        this.news = news;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        final News news = this.news.get(position);

        holder.newsTitle.setText(news.getTitle());
        holder.newsTime.setText(news.getTime());

        holder.newsBrief.setText(news.getBrief());

        if (StringUtils.isNotBlank(news.getImageUrl())) {
            LinearLayout.LayoutParams textLayoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT,8F);
            textLayoutParams.rightMargin = 10;
            holder.newsBrief.setLayoutParams(textLayoutParams);
            LinearLayout.LayoutParams imageLayoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT,2F);
            imageLayoutParams.leftMargin = 10;
            holder.newsImage.setLayoutParams(imageLayoutParams);
            GlideApp.with(context)
                    .load(news.getImageUrl())
                    .centerCrop()
                    .placeholder(R.mipmap.ic_placeholder)
                    .into(holder.newsImage);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, news.getId().toString(), Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(context, NewsDetailActivity.class);
                //启动
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return news.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.news_title)
        TextView newsTitle;

        @BindView(R.id.news_time)
        TextView newsTime;

        @BindView(R.id.news_brief)
        TextView newsBrief;

        @BindView(R.id.news_image)
        ImageView newsImage;

        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
